package com.ruoyi.project.system.visit.controller;

import java.util.List;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.project.system.doctor.domain.TableDoctor;
import com.ruoyi.project.system.doctor.service.ITableDoctorService;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.visit.domain.TableVisit;
import com.ruoyi.project.system.visit.service.ITableVisitService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 就诊信息Controller
 * 
 * @author ruoyi
 * @date 2023-09-13
 */
@Controller
@RequestMapping("/system/visit")
public class TableVisitController extends BaseController
{
    private String prefix = "system/visit";
    @Autowired
    private ITableDoctorService tableDoctorService;
    @Autowired
    private ITableVisitService tableVisitService;

    @RequiresRoles(value = {"manager","doctor"},logical =  Logical.OR)
    @GetMapping()
    public String visit()
    {
        return prefix + "/visit";
    }

    /**
     * 查询就诊信息列表
     */
    @RequiresRoles(value = {"manager","doctor"},logical =  Logical.OR)
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TableVisit tableVisit)
    {
        boolean admin = ShiroUtils.getSysUser().isManager();
        if(!admin){
            tableVisit.setDoctorName(ShiroUtils.getSysUser().getUserName());
        }
        startPage();
        List<TableVisit> list = tableVisitService.selectTableVisitList(tableVisit);
        return getDataTable(list);
    }

    /**
     * 导出就诊信息列表
     */
    @RequiresRoles(value = {"manager","doctor"},logical =  Logical.OR)
    @Log(title = "就诊信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TableVisit tableVisit)
    {
        List<TableVisit> list = tableVisitService.selectTableVisitList(tableVisit);
        ExcelUtil<TableVisit> util = new ExcelUtil<TableVisit>(TableVisit.class);
        return util.exportExcel(list, "就诊信息数据");
    }

    /**
     * 新增就诊信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存就诊信息
     */
    @RequiresRoles({"doctor"})
    @Log(title = "就诊信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TableVisit tableVisit)
    {
        String userName = ShiroUtils.getSysUser().getUserName();
        tableVisit.setSuoying(IdUtils.fastSimpleUUID());
        tableVisit.setBinglibh(DateUtils.getDate().replace("-","")+ IdUtil.fastSimpleUUID().substring(0,6));
        TableDoctor query = new TableDoctor();
        query.setName(userName);
        TableDoctor tableDoctor = tableDoctorService.selectTableDoctorList(query).get(0);
        tableVisit.setDoctorNum(tableDoctor.getGonghao());
        tableVisit.setDoctorName(tableDoctor.getName());
        tableVisit.setDoctorKeshi(tableDoctor.getKeshi());
        return toAjax(tableVisitService.insertTableVisit(tableVisit));
    }

    /**
     * 修改就诊信息
     */
    @RequiresRoles({"doctor"})
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TableVisit tableVisit = tableVisitService.selectTableVisitById(id);
        mmap.put("tableVisit", tableVisit);
        return prefix + "/edit";
    }

    /**
     * 修改保存就诊信息
     */
    @RequiresRoles({"doctor"})
    @Log(title = "就诊信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TableVisit tableVisit)
    {
        return toAjax(tableVisitService.updateTableVisit(tableVisit));
    }

    /**
     * 删除就诊信息
     */
    @RequiresRoles({"doctor"})
    @Log(title = "就诊信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tableVisitService.deleteTableVisitByIds(ids));
    }
}
